<template>
    <a-drawer
        id="add_machine_drawer"
        width="60%"
        title="权限设置"
        placement="right"
        :visible="modal_show"
        @close="onCancle"
        :destroyOnClose="true"
        :body-style="{ paddingBottom: '80px' }"
        :footer-style="{ textAlign: 'right' }"
        >
        <a-tabs
            v-model:activeKey="activeKey"
            tab-position="left"
            :style="{ height: '200px' }"
            >
            <a-tab-pane v-for="(item,index) in user_levels" :key="index" >
                <template #tab>
                    <span>{{ item?.fullname }}</span>
                </template>
                <!-- <a-checkbox v-model:checked="item.level[0]">超级权限</a-checkbox> -->
                <a-checkbox v-model:checked="item.level[1]">删除权限</a-checkbox>
                <a-checkbox v-model:checked="item.level[2]">写权限</a-checkbox>
                <a-checkbox v-model:checked="item.level[3]">读权限</a-checkbox>
            </a-tab-pane>
        </a-tabs>
        <template #extra>
            <a-space>
                <a-button @click="onCancle">取消</a-button>
                <a-button type="primary" @click="onConfirm">确认</a-button>
            </a-space>
        </template>
    </a-drawer>
</template>
<script>
export default {
    data() {
        return {
            modal_show: false,
            activeKey:0,
            user_levels:[],
        };
    },
    beforeUnmount() {
        this.$mitt.off('edit_machine_rome_person_level_drawer');
    },
    mounted() {
        this.$mitt.on('edit_machine_rome_person_level_drawer', _value => {
            this.modal_show=true;
            this.user_levels=[]
            for (let i = 0; i < _value.length; i++) {
                let element = _value[i];
                let level=element?.level.split("")
                let level_bool=[]
                for (let j = 0; j < level.length; j++) {
                    if(level[j]=="1"){
                        level_bool.push(true)
                    }
                    else{
                        level_bool.push(false)
                    }
                }
                element.level=level_bool
                this.user_levels.push(element)
            }
        });
    },
    methods: {
        onCancle(){
            this.modal_show=false
        },
        async onConfirm(){
            let new_user_levels=[]
            for (let i = 0; i < this.user_levels?.length; i++) {
                let element =  this.user_levels[i];
                let level_bool=element.level
                let levels=""
                for (let j = 0; j < level_bool.length; j++) {
                    if(level_bool[j]===true){
                        levels=levels+"1"
                    }
                    else{
                        levels=levels+"0"
                    }
                }
                element.level=levels
                new_user_levels.push(element)
            }
            this.$mitt.emit('edit_machine_rome_person_level_drawer_result', new_user_levels);
            this.modal_show=false
        },
    },
};
</script>